<?php
/**
 * DouPHP小程序模块
 * Author: cuihaifeng  qq 366131726 微信同号
 * 网址：www.zbxingke.com www.0533hf.com
 */
if (!defined('IN_DOUCO_API')) {
    die('Access Denied');
}

/**
 * 插入表单
 */
function doPageInsert(){
    global $_W,$_GPC,$_MODULE,$_CFG,$_LANG,$check;

    if(!$_MODULE['open']['guestbook'])
    {
        return wxapp_result(404,'留言模块不存在！');
    }

    // 如果限制必须输入中文则修改错误提示
    $include_chinese = $_CFG['guestbook_check_chinese'] ? $_LANG['guestbook_include_chinese'] : '';
    $state = $_GPC['state'];
    $openid = substr($state,strpos($state,'dousid-'));
    $title = '小程序留言-'.$openid;
    $name = $_GPC['name'];
    $tel = $_GPC['tel'];
    $content = $_GPC['content'];
    if ($check->is_illegal_char($name)) {
        $wrong['name'] = $_LANG['guestbook_name'] . $_LANG['illegal_char'];
    } elseif (!check_guestbook($name, 200)) {
        $wrong['name'] = preg_replace('/d%/Ums', $include_chinese, $_LANG['guestbook_name_wrong']);
    }

    if (!$check->is_telphone($tel)){
        $wrong['contact'] = $_LANG['guestbook_tel_wrong'];
    }
    if ($check->is_illegal_char($content)) {
        $wrong['content'] = $_LANG['guestbook_content'] . $_LANG['illegal_char'];
    } elseif (!check_guestbook($content, 300)) {
        $wrong['content'] = preg_replace('/d%/Ums', $include_chinese, $_LANG['guestbook_content_wrong']);
    }

    $wrong_format = '';
    if ($wrong) {
        foreach ($wrong as $key => $value) {
            $wrong_format .= $wrong[$key] . '&';
        }
       return wxapp_result(404,$wrong_format);
    }
    $add_time = time();

    pdo_insert('guestbook',array('title'=>$title,'contact_type'=>'tel','contact'=>$tel,'name'=>$name,'content'=>$content,'add_time'=>$add_time));

    return wxapp_result(0,'留言成功,我们会尽快与您取得联系!');


}

/**
 * +----------------------------------------------------------
 * 检查是否包含中文字符且长度符合要求
 * +----------------------------------------------------------
 */
function check_guestbook($value, $length) {
    $check_chinese = $GLOBALS['_CFG']['guestbook_check_chinese'] ? $GLOBALS['check']->if_include_chinese($value) : true;

    if ($check_chinese && $GLOBALS['check']->ch_length($value, $length)) {
        return true;
    }
}